package com.fastloan.mgt.home.service.impl;

import com.fastloan.common.BasicServiceImpl;
import com.fastloan.mgt.home.model.HomeConfig;
import com.fastloan.mgt.home.service.HomeConfigService;
import com.fastloan.mgt.product.basics.service.ProductService;
import com.kawins.supers.file.core.FileLoadBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import tk.mybatis.mapper.entity.Example;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yanwe
 * createTime 2017-11-2017/11/16 14:55
 */
@Service
public class HomeConfigServiceImpl extends BasicServiceImpl<HomeConfig> implements HomeConfigService {

    @Autowired
    private ProductService productService;

    private static final Integer HOT_PRODUCT = 0;
    private static final Integer AD = 1;

    public static final Integer SHOW = 1;
    public static final Integer DON_T_SHOW = 0;

    @Override
    public void addHomeConfig(List<HomeConfig> homeConfigList){
        if (homeConfigList == null || homeConfigList.isEmpty()){
            return;
        }
        String orgId = homeConfigList.get(0).getOrgId();
        HomeConfig criteria = new HomeConfig();
        criteria.setOrgId(orgId);
        deleteFile(homeConfigList, criteria);
        delete(criteria);
        homeConfigList.forEach(this::insertSelective);
    }

    @Override
    public void modifyShowConfig(String productId,Integer isShow){
        HomeConfig homeConfig = new HomeConfig();
        homeConfig.setIsShow(isShow);
        Example example = new Example(HomeConfig.class);
        example.createCriteria().andEqualTo("productId",productId);
        updateByExampleSelective(homeConfig,example);
    }

    @Override
    public Map<String, List<HomeConfig>> getHomeConfig(List<String> orgIds){
        return getHomeConfig(orgIds,SHOW);
    }

    @Override
    public Map<String, List<HomeConfig>> getHomeConfig(List<String> orgIds,Integer show){
        List<HomeConfig> homeConfigList = new ArrayList<>();
        for (String orgId : orgIds) {
            HomeConfig criteria = new HomeConfig();
            criteria.setOrgId(orgId);
            criteria.setIsShow(show);
            homeConfigList.addAll(select(criteria));
        }
        if (homeConfigList.isEmpty()){
            return null;
        }
        homeConfigList.forEach(homeConfig -> homeConfig.setProduct(
                productService.selectByPrimaryKey(homeConfig.getProductId())));
        Map<Integer, List<HomeConfig>> groupHomePage = homeConfigList.stream()
                .collect(Collectors.groupingBy(HomeConfig::getType));
        Map<String,List<HomeConfig>> map = new HashMap<>();
        List<HomeConfig> ADList = groupHomePage.get(AD);
        if (ADList != null){
            ADList.sort(Comparator.comparing(HomeConfig::getSort));
        }
        map.put("AD",ADList);
        List<HomeConfig> hotProductList = groupHomePage.get(HOT_PRODUCT);
        if (hotProductList != null){
            hotProductList.sort(Comparator.comparing(HomeConfig::getSort));
        }
        map.put("hotProduct",hotProductList);
        return map;
    }

    @Override
    public List<FileLoadBean> uploadFile(MultipartFile[] multipartFile) {
        return fileManager.upload(multipartFile);
    }

    private void deleteFile(List<HomeConfig> homeConfigList, HomeConfig criteria) {
        List<HomeConfig> oldConfigList = select(criteria);
        for (HomeConfig oldConfig : oldConfigList) {
            if (StringUtils.isBlank(oldConfig.getFileId())){
                continue;
            }
            boolean flag = true;
            for (HomeConfig homeConfig : homeConfigList) {
                if (StringUtils.isBlank(homeConfig.getFileId())){
                    continue;
                }
                if (homeConfig.getFileId().equals(oldConfig.getFileId())){
                    flag = false;
                }
            }
            if (flag && oldConfig.getFileId() != null){
                fileManager.delete(oldConfig.getFileId());
            }
        }
    }
}
